home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / lib / util / sstr2arg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-02-01  |  2.4 KB  |  102 lines

  1.  
  2. #include "util.h"
  3.  
  4. /*  convert string into argument list
  5.  *
  6.  *    This version takes an array of delimiters.    <DPK@BRL>
  7.  *
  8.  *  stash a pointer to each field into the passed array.
  9.  *  any common seperators split the words.  extra white-space
  10.  *  between fields is ignored.
  11.  *
  12.  *  specially-interpreted characters:
  13.  *      double-quote, backslash (preceding a special char with a
  14.  *    backslash removes its interpretation.  A backslash not
  15.  *    followed by a special is used to preface an octal specification
  16.  *    for one character a string begun with double-quote has only
  17.  *      double-quote and backslash as special characters.
  18.  */
  19.  
  20. extern int errno;
  21.  
  22. /* convert srcptr to argument list */
  23. sstr2arg (srcptr, max, argv, dlmstr)
  24. register char *srcptr;    /* source data */
  25. int max;        /* maximum number of permitted fields */
  26. char *argv[];        /* where to put the pointers */
  27. char *dlmstr;        /* Delimiting character */
  28. {
  29.     char gotquote;      /* currently parsing quoted string */
  30.     register int ind;
  31.     register char *destptr;
  32.  
  33.     if (srcptr == 0) {
  34.     errno = EINVAL;     /* emulate system-call failure */
  35.     return (NOTOK);
  36.     }
  37.  
  38.     for (ind = 0, max -= 2;; ind++) {
  39.     if (ind >= max) {
  40.         errno = E2BIG;      /* emulate system-call failure */
  41.         return (NOTOK);
  42.     }
  43.  
  44.         /* Skip leading white space */
  45.         for (; *srcptr == '\t' || *srcptr == ' '; srcptr++);
  46.  
  47.     argv [ind] = srcptr;
  48.     destptr = srcptr;
  49.  
  50.     for (gotquote = FALSE; ; ) {
  51.         register char *cp;
  52.  
  53.         for (cp = dlmstr; (*cp != '\0') && (*cp != *srcptr); cp++);
  54.  
  55.         if (*cp != '\0')
  56.         {
  57.         if (gotquote) {           /* don't interpret the char */
  58.             *destptr++ = *srcptr++;
  59.             continue;
  60.         }
  61.  
  62.         srcptr++;
  63.         *destptr = '\0';
  64.         goto nextarg;
  65.         } else {
  66.         switch (*srcptr) {
  67.         default:        /* just copy it                     */
  68.             *destptr++ = *srcptr++;
  69.             break;
  70.  
  71.         case '\"':      /* beginning or end of string       */
  72.             gotquote = (gotquote) ? FALSE : TRUE;
  73.             srcptr++;   /* just toggle */
  74.             break;
  75.  
  76.         case '\\':      /* quote next character             */
  77.             srcptr++;   /* just skip the back-slash         */
  78.             if (*srcptr >= '0' && *srcptr <= '7') {
  79.                 *destptr = '\0';
  80.                 do
  81.                     *destptr = (*destptr << 3) | (*srcptr++ - '0');
  82.                 while (*srcptr >= '0' && *srcptr <= '7');
  83.                 destptr++;
  84.                 break;
  85.             }
  86.             else
  87.                  *destptr++ = *srcptr++;
  88.             break;
  89.  
  90.             case '\0':
  91.             *destptr = '\0';
  92.             ind++;
  93.             argv[ind] = (char *) 0;
  94.             return (ind);
  95.         }
  96.         }
  97.     }
  98.     nextarg:
  99.     continue;
  100.     }
  101. }
  102.